package cn.itsource.pet.service.impl;

import cn.itsource.basic.service.impl.BaseServiceImpl;
import cn.itsource.basic.util.AjaxResult;
import cn.itsource.basic.util.PageList;
import cn.itsource.pet.mapper.PetMapper;
import cn.itsource.pet.pojo.domain.Pet;
import cn.itsource.pet.query.PetQuery;
import cn.itsource.pet.service.IPetService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
//Propagation.SUPPORTS:支持别人事务
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class PetServiceImpl extends BaseServiceImpl<Pet, PetQuery> implements IPetService {
    
    
    @Autowired
    private PetMapper petMapper;
    
    /**
     * 分页
     * @param query 高级查询条件
     */
    @Override
    public PageList<Pet> findPage(PetQuery query) {
        //1.分页插件
        PageHelper.startPage(query.getPage(),query.getSize());
        //2.查询
        List<Pet> petPageList = petMapper.pageList(query);
        //3.封装pageInfo对象
        PageInfo<Pet> pageInfo = new PageInfo<>(petPageList);

        //4.封装自定义的分页对象
        PageList<Pet> pageList = new PageList<>(pageInfo.getTotal(), pageInfo.getList());

        return pageList;
    }

    @Override
    public PageList<Pet> queryPetPage(PetQuery petQuery) {
        //1.分页插件
        PageHelper.startPage(petQuery.getPage(),petQuery.getSize());
        //2.查询
        List<Pet> petPageList = petMapper.queryPetPage();

        PageInfo<Pet> petPageInfo = new PageInfo<>(petPageList);
        //4.封装自定义的分页对象
        PageList<Pet> pageList = new PageList<>(petPageInfo.getTotal(), petPageInfo.getList());

        return pageList;
    }

    @Override
    public AjaxResult upDown(PetQuery petQuery) {

        petMapper.upDown(petQuery);

        return AjaxResult.createSuccess();
    }
}
